package com.dongdong.bookstore.common.api;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * <p>统一返回类型</p>
 *
 * @author chiangkai
 * @date 2021-10-14
 * @email chiangkk@163.com
 */
@Data
@AllArgsConstructor
public class ResultData<T> {
    /**
     * 结果状态 ,具体状态码参见ResultCode.java
     */
    private int status;
    private String message;
    private T data;
    private long timestamp;

    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ResultData<T> ok(String message) {
        return ok(message, null);
    }

    public static <T> ResultData<T> ok(String message, T data) {
        return ResultData.result(ReturnCode.RC200.getCode(), message, data);
    }


    public static <T> ResultData<T> success(T data) {
        return result(ReturnCode.RC200.getCode(), ReturnCode.RC200.getMessage(), data);
    }

    public static <T> ResultData<T> fail(int code, String message) {
        return result(code, message, null);
    }

    public static <T> ResultData<T> fail(String message) {
        return fail(ReturnCode.RC500.getCode(), message);
    }

    public static <T> ResultData<T> result(int code, String message, T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(code);
        resultData.setMessage(message);
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> validateFailed(String message){
        return validateFailed(message,null);
    }

    public static <T> ResultData<T> validateFailed(String message,T data){
        return result(ReturnCode.VALIDATE_FAILED.getCode(),message,data);
    }



}
